package com.jz.pay3.config;

import com.github.kristofa.brave.Brave;
import com.github.kristofa.brave.mysql.MySQLStatementInterceptorManagementBean;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisClusterConfiguration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisNode;
import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
//import org.springframework.boot.autoconfigure.data.redis.LettuceConnectionConfiguration;


import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Configuration
public class ApplicationContextConfig {

    @Bean
    public RestTemplate getRestTemplate(){
        return new RestTemplate();
    }

    @Value("${spring.redis.host}")
    private String redisHost;
    @Value("${spring.redis.port}")
    private int redisPort;
    @Value("${spring.redis.password}")
    private String redisPassword;
    @Value("${spring.redis.timeout}")
    private int redisTimeout;


    @Bean
    public JedisPool getJedisPool(JedisPoolConfig JedisPoolConfig){

        String host = redisHost;
        int port = redisPort;
        int timeout = redisTimeout;
        String password = redisPassword;
        JedisPool jedisPool = new JedisPool(JedisPoolConfig,host,port,timeout,password);
        return jedisPool;
    }

    @ConfigurationProperties(prefix = "spring.redis.jedis")
    @Bean
    public JedisPoolConfig getJedisPoolConfig(){
        JedisPoolConfig JedisPoolConfig = new JedisPoolConfig();
        JedisPoolConfig.setTestOnBorrow(false);
        JedisPoolConfig.setTestOnReturn(false);
        JedisPoolConfig.setTestOnCreate(true);
        return JedisPoolConfig;
    }

    @Bean
    public MySQLStatementInterceptorManagementBean mySQLStatementInterceptorManagementBean() {
        return new MySQLStatementInterceptorManagementBean(new Brave.Builder().build().clientTracer());
    }
//
//    @Bean
////    @Qualifier("redis-write")
//    public RedisTemplate<String, Object> getRedisTemplate(RedisConnectionFactory redisConnectionFactory){
//        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
//        redisTemplate.setConnectionFactory(redisConnectionFactory);
//        return redisTemplate;
//    }


//    @Bean
//    public RedisTemplate<String, Object> getRedisTemplate2(){
////        LettuceClientConfiguration config = LettuceClientConfiguration.builder().build();
//        RedisClusterConfiguration redisClusterConfiguration = new RedisClusterConfiguration();
//        List<RedisNode> list = new ArrayList<>();
//        for (MyRedisProperties.Node map : myRedisProperties.getRedisNodes()) {
//            RedisNode node = new RedisNode.RedisNodeBuilder()
//                    .listeningAt(map.getIp(), map.getPort())
//                    .promotedAs(RedisNode.NodeType.valueOf(map.getType())).build();
//            BeanUtils.copyProperties(map,node);
//
//            list.add(node);
//        }
//        redisClusterConfiguration.setClusterNodes(list);
//        redisClusterConfiguration.setPassword("123456");
//        LettuceConnectionFactory factory = new LettuceConnectionFactory(redisClusterConfiguration);
//        //        factory.setHostName(myRedisProperties.getHost());
////        factory.setPort(myRedisProperties.getPort());
////        factory.setDatabase(myRedisProperties.getDatabase());
////        factory.setPassword(myRedisProperties.getPassword());
//
////        factory.set
//
//        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
//        redisTemplate.setConnectionFactory(factory);
//        return redisTemplate;
//    }
}
